package com.fmsz.xmly.fragments.findfgts;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fmsz.xmly.R;
import com.fmsz.xmly.adapters.findrecom.FindRecomAdapter;
import com.fmsz.xmly.asynctasks.findrecom.FindRecomVP1Down;
import com.fmsz.xmly.asynctasks.findrecom.FindRecomVP2Down;
import com.fmsz.xmly.callbacks.InterfaceClass;
import com.fmsz.xmly.entitys.findrecom.FindRecomVP1Entity;
import com.fmsz.xmly.entitys.findrecom.FindRecomVP2Entity;
import com.fmsz.xmly.utils.Urls;

import java.util.ArrayList;
import java.util.List;

public class FindRecommendFragment extends Fragment implements InterfaceClass.FindRecomVP1Callback,InterfaceClass.FindRecomVP2Callback {

    private FindRecomAdapter adapter;
    private RecyclerView recycler;
    private List<FindRecomVP1Entity> vp1List = new ArrayList<>();
    private List<FindRecomVP2Entity> vp2List=new ArrayList<>();
    public FindRecommendFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_find__recommend_, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recycler = (RecyclerView) view.findViewById(R.id.find_recommend_rv);
        startDownload();
        //TODO 给adapter传递数据
        adapter = new FindRecomAdapter(getActivity(), vp1List,vp2List);
        recycler.setAdapter(adapter);
    }

    //开启异步下载
    private void startDownload() {
        new FindRecomVP1Down(this).execute(Urls.FIND_INTERFACE_1);
        new FindRecomVP2Down(this).execute(Urls.FIND_INTERFACE_1);
    }

    //发现-推荐-VP1,接口回调
    @Override
    public void findRecomVP1Callback(List<FindRecomVP1Entity> list) {
        if (vp1List.size()>0) {
            vp1List.clear();
        }
        vp1List.addAll(list);
        adapter.notifyDataSetChanged();
    }
    //发现-推荐-VP2,接口回调
    @Override
    public void findRecomVP2Callback(List<FindRecomVP2Entity> list) {
        if (vp2List.size()>0) {
            vp2List.clear();
        }
        vp2List.addAll(list);
        adapter.notifyDataSetChanged();
    }
}
